<?php 
/**
* 
*/
class reportTemplate
{
	
	function __construct()
	{
		
	}
	function comboTgl(){
		$s = '';
		for ($tgl=1; $tgl < 32; $tgl++) { 
			$chk = date('d')==$tgl ?"selected='selected'":'';
			$s .='<option '.$chk.' value="'.$tgl.'">'.$tgl.'</option>';
		}
		return $s;
	}
	function comboBln(){
		$bln = array('01' =>'Januari' ,
			'02'=>'Februari',
			'03'=>'Maret',
			'04'=>'April',
			'05'=>'Mei',
			'06'=>'Juni',
			'07'=>'Juli',
			'08'=>'Agustus',
			'09'=>'September',
			'10'=>'Oktober',
			'11'=>'November',
			'12'=>'Desember',
			);
		$s = '';
		foreach ($bln as $key => $value) {
			$slC = date('m')==$key ?"selected='selected'":'';
			$s .='<option '.$slC.' value="'.$key.'">'.$value.'</option>';
		}
		return $s;
	}
	function comboThn($jarak=10){
		$min = date('Y')-$jarak;
		$max= date('Y')+$jarak;
		$s= '';

		for ($i=$min; $i <=$max ; $i++) { 
			$slC = date('Y')==$i ?"selected='selected'":'';
			$s .='<option '.$slC.' value="'.$i.'">'.$i.'</option>';
		}
		return $s;
	}
	function mainTemplate($headTitle,$bodyContent){
		$r='
		<html>
		<head>
		<title>'.$headTitle.'</title>
		<link rel="stylesheet" type="text/css" href="'.BASEURL.'css/report.css">
		<link rel="stylesheet" type="text/css" href="'.BASEURL.'css/isi.css">
		</head>
		<body>'.$bodyContent.'</body>
		</html>
		';
		return $r;
	}
	function lapStokBrg($data,$whereToDisplay=null){
		$stringToReturn = '';
		$judul="Laporan Stok dan Harga Barang";
		$stringToReturn .= '<h2>'.$judul.'</h2>';
		if(is_array($whereToDisplay)){
			foreach ($whereToDisplay as $wtDK => $wtDV) {
				$stringToReturn .='<p>'.$wtDK.' : '.$wtDV.'</p>';
			}
		}
		
		$stringToReturn .='<div class="frame_table_order">
		<p>Tanggal Cetak: '.date('d F Y H:i:s').'</p>
<table width="100%" cellpadding="5" cellspacing="0" border="1" style="border-collapse:collapse">';
		$stringToReturn .='<tr align="center" valign="middle" bgcolor="#CCCCCC">
		<td>No</td>
		<td>Nama Produk</td>
		<td>Kategori</td>
		<td>Harga Beli</td>
		<td>Harga Jual</td>
		<td>Sisa Stok</td>
		</tr>';
		if($data['success']==false || ($data['success']==true && $data['total']==0)){
			$stringToReturn .= '<tr><td colspan="7" align="center">(data tidak ada)</td></tr>';
		}
		else{
		//	$stringToReturn .=" <h1>Laporan Stok Barang</h1>";
			
			$no = 1;
			foreach ($data['result'] as $dtReport) {
				$stringToReturn .="<tr><td>".$no++;
				$stringToReturn .="</td><td>".$dtReport['nama'];
				$stringToReturn .="</td><td>".$dtReport['nama_kategori']."</td>";
				$stringToReturn .="<td align='right'>".(number_format($dtReport['h_beli'],0,',','.'))."</td>
				<td align='right'>".(number_format($dtReport['h_jual'],0,',','.'))."</td><td>".$dtReport['jumlah']."</td></tr>";
				//$stringToReturn .= $dtReport['nama'].'<br />';
			}
			$stringToReturn .="</table></div>";
		}
		return $this->mainTemplate($judul,$stringToReturn);
	}
	function comboStatus(){
		$arrS = array('TUNGGU_PEMBAYARAN' =>'Menunggu Pembayaran',
        'DITOLAK'=>'Ditolak','PENGIRIMAN'=>'Dalam Pengiriman','SELESAI'=>'Transaksi Selesai');
        $s = '';
        foreach ($arrS as $key => $value) {
        	$s .="<option value='".$key."'>".$value."</option>";
        };
        return $s;
	}

    function formatUang($angka,$mtUang = 'Rp.'){
        return $mtUang.number_format($angka,0,',','.');
    }
	function lapPenjualan($data,$date,$additional=array(),$stat ){
		$no=1;

			$totHBeli=0;
			$totHJual=0;
			$totQty=0;
			$totLaba=0;
		require_once '../kelas/reports.php';
		$objReports=new reports();
		$objFungsi=new fungsi();
		//error_reporting(0);
		$strResult = '';
		$addTD = '';
		if($stat=='all'){
			$addTD ='<td>Status</td>';
		};
		$judul = 'Laporan Penjualan';
		$strResult .='<h2>'.$judul.'</h2>';
				$strResult .='<p>Tanggal : '.$date['from'].' s/d '.$date['to'].'</p>';	

		if(count($additional)>0){
			foreach ($additional as $key => $value) {
				$strResult .='<p>'.$key.' : '.$value.'</p>';	
			}
			
		}
		$strResult .= '<div class="frame_table_order">
<table width="100%" cellpadding="5" cellspacing="0" border="1" style="border-collapse:collapse">
<thead>
	<tr align="center" valign="middle" bgcolor="#CCCCCC">
<td>No</td>
			
			<td>Nama Barang</td>
			<td>Harga Beli</td>
			<td>Diskon</td>
			<td>Harga Jual</td>
			<td>Qty</td>
			<td>Laba</td>
			
		</tr>
		</thead>';
		if($data['success']==false||($data['success']==true && $data['total']==0)){
			$strResult .= '<tr><td align="center" colspan="7">(tidak ada data)</td></tr>';
		}else{
			foreach ($data['result'] as $key => $value) {
				$strResult .="<tr><td colspan='7' align='center'>Nomor Transaksi: <b>".$value['id_pemesanan']."</b> | 
				Tanggal: ".$value['tanggal']." | Pemesan: ".$value['pemesan']." | Pemproses: ".$value['nama']." 
				| Alamat: ".$value['alamat']." | Status: ".$value['status_pesan']."</td>
				</tr>";

				$dtDetail=$objReports->getDetailPenjualan($value['id_pemesanan']);
				if($dtDetail['success']==false||($dtDetail['success']==true && $dtDetail['total']==0)){
					$strResult .='<tr><td colspan="7" align="center">(tidak ada data detail untuk transaksi ini)</tr>';
				}
				else{
					//var_dump($dtDetail['result'][0]);
					foreach ($dtDetail['result'] as $kDet => $vDet) {
						$totQty +=$vDet['jumlah_buy'];
						$strResult .='<tr><td>'.$no++.'</td>
						<td>'.$vDet['nama'].'</td>
						<td align="right">'.($objFungsi->formatUang($vDet['h_beli'])).'</td>
						<td align="right">'.$vDet['diskon'].'%</td>';
						$hJualnYa =($vDet['h_jual']-($vDet['h_jual']*($vDet['diskon']/100)));
						$strResult .='<td align="right">'.($objFungsi->formatUang($hJualnYa)).'</td>
						<td align="right">'.$vDet['jumlah_buy'].'</td>';
						$laBaSide =($vDet['h_jual']-($vDet['h_jual']*($vDet['diskon']/100)) * $vDet['jumlah_buy']);
						$totLaba +=$laBaSide;
						$totHJual +=$hJualnYa;
						$totHBeli +=$vDet['h_beli'];
						$strResult .='<td align="right">'.$objFungsi->formatUang($laBaSide).'</td></tr>';
					}
				};
				$strResult .='<tr><td colspan="7" align="center" style="background:#e3e3e3">&nbsp;</tr>';
			}
		};

		
		$strResult .= '<tr style="font-weight:bold"><td align="center" colspan="2">Total:</td>
		<td align="right">'.($this->formatUang($totHBeli)).'</td>
		<td></td>
		<td align="right" >'.($this->formatUang($totHJual)).'</td><td align="right" >'.($totQty).'</td>
		
		<td align="right" >'.($this->formatUang($totLaba)).'</td> </tr>';
 		$strResult .= '</table></div>';
		return $this->mainTemplate($judul,$strResult);
	}
}
$objReportTemplate = new reportTemplate();